package com.ys.web.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.ys.web.dao.CommonDao;
import com.ys.web.entity.Backup;
import com.ys.web.entity.Role;
import com.ys.web.entity.User;
import com.ys.web.entity.UserRole;
import com.ys.web.pojo.BackupDescription;
import com.ys.web.pojo.BackupInfo;
import com.ys.web.pojo.UserInfo;
import com.ys.web.service.BackupService;

@Service("backupService")
@Transactional
public class BackupServiceImpl implements BackupService{

	
	@Autowired
	private CommonDao commonDao;
	
	
	public List<BackupInfo> getBackupInfo() {
		List<Backup> backups = new ArrayList();
		
		List<BackupInfo> backupInfos = new ArrayList();
		
		
		
		
		backups = commonDao.findHql("from Backup where isGood=1 ");
		for(Backup b : backups){
			BackupInfo backupInfo = new BackupInfo();
			backupInfo.setId(b.getId());
			backupInfo.setCompanyId(b.getCompanyId());
			backupInfo.setCompanyName(b.getCompanyName());
			backupInfo.setPath(b.getPath());
			backupInfo.setIsGood(b.getIsGood());
			backupInfo.setCreateTime(b.getCreateTime());
			backupInfos.add(backupInfo);
		}
		
		
		return backupInfos;
}

	public List<String> getBackupInfoById(Integer id) {
		List<String> paths = new ArrayList();
		String hql = "select path from Backup where companyId=?";
		paths = commonDao.findHql(hql, id);
		
		return paths;
}
	

	@Override
	public void saveBackupInfo(BackupInfo backupInfo) {
		Backup backup = new Backup();
		BeanUtils.copyProperties(backupInfo,backup);
		
		commonDao.save(backup);
	}


	@Override
	public String getCompanyName(Integer id) {
		
		return commonDao.getCompanyName(id);
	}


	@Override
	public void deleteCompanyBackup(Integer id) {
		
		
	}

	@Override
	public Integer getBackupHasPath(Integer id) {
		Integer result = 0;
		String hql = "select path from Backup where companyId=?";
		List<String> hasPath = commonDao.findHql(hql, id);
		if(!hasPath.isEmpty()){
			
				result = 1;
			
		}
		return result;
		
		
	}
	
	@Override
	public String getBackupIsGood(Integer id) {
		String result = "普通";
		String hql = "select isGood from Backup where companyId=?";
		List<Integer> isGood = commonDao.findHql(hql, id);
		if(!isGood.isEmpty()){
			
			if(Integer.valueOf(isGood.get(0))==1){
				result = "优秀";
			}
		}
		return result;
		
		
	}

	

	@Override
	public List<BackupDescription> getBackupDescription(Integer id) {
		List<BackupDescription> backupDescriptionList = new ArrayList();
		
		
		
		String hql = " from Backup where companyId=?";
		List<Backup> backups =  commonDao.findHql(hql, id);
		if(!backups.isEmpty()){
		for(Backup b :backups){
			BackupDescription backupDescription = new BackupDescription();
			backupDescription.setBackupId(b.getId());
			
			backupDescription.setDescription(b.getDescription());
			String sign = "普通";
			if(b.getIsGood()==1){
				sign = "优秀";
			}
			backupDescription.setIsGood(sign);
			SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			String createTime = sdf.format(b.getCreateTime());
			
			backupDescription.setCreateTime(createTime);
			backupDescriptionList.add(backupDescription);
		}
		return backupDescriptionList;
		}
		return null;
	}
	
	public Integer getIdByDescription(String description){
		Integer id = null;
		String sql = "from Backup where description=?";
		List<Backup> backupList = commonDao.findHql(sql, description);
		
		if(!backupList.isEmpty()){
			for(Backup b :backupList){
				id = b.getCompanyId();
			}
		}
		
		
		return id;
		
	}

	@Override
	public Integer signGoodBackup(Integer id) {
		String hql = "update Backup set isGood = 1 where id="+id;
		
		
		return commonDao.executeHql(hql);
	}

	@Override
	public Integer signCommonBackup(Integer id) {
		
		String hql = "update Backup set isGood = 0 where id="+id;
		
		
		return commonDao.executeHql(hql);
	
	}

	@Override
	public String downloadBackup(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Integer id) throws IOException{
	
		String paths = null;
		String hql = "from Backup where id=?";
		List<Backup> backupList = commonDao.findHql(hql, id);
		
		if(!backupList.isEmpty()){
			
		
		for(Backup b :backupList){
			paths = b.getPath();
		}
		}
		try {
			
			
			
			String filename = paths;
	        //设置Content-Disposition  
			httpServletResponse.setHeader("Content-Disposition", "attachment;filename="+filename);  
			httpServletResponse.setContentType("application/octet-stream ");  
			File file = new File(paths);
			
			
			
	        //读取目标文件，通过response将目标文件写到客户端  
	        //获取目标文件的绝对路径  
	        String fullFileName = file.getAbsolutePath();
	        //System.out.println(fullFileName);  
	        //读取文件  
	        InputStream in = new FileInputStream(fullFileName);  
	        OutputStream out = httpServletResponse.getOutputStream();  
	        int b;  
	        while((b=in.read())!= -1)  
	        {  
	            out.write(b);  
	        }  
	          
	        in.close();  
	        out.close(); 
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			
		}
		return null;
	}
	
}
